<template>
  <div class="app-container">
    <SearchContent :queryParams="queryParams" labelWidth="145px" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="车牌号/车架号:">
          <el-input v-model="queryParams.carNoVinNo" clearable placeholder="请输入车牌号/车架号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车辆产权:">
          <el-select filterable v-model="queryParams.propertyRightId" placeholder="请选择车辆产权" clearable
                     style="width: 100%">
            <el-option v-for="item in companyOriginList" :label="item.deptName" :value="item.deptId"
                       :key="item.deptId"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌/车系/车型:">
          <linkage-select @changeNode="linkageChange" v-model="linkageList" placeholder="请选择品牌/车系/车型"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车辆状态:">
          <el-select v-model="queryParams.businessType" placeholder="请选择车辆状态">
            <el-option v-for="item in dict.type.car_business_type" :label="item.label" :value="item.value"
                       :key="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="商业险保险公司:">
          <el-select v-model="queryParams.commercialCompany" style="width: 100%" placeholder="请选择商业险保险公司">
            <el-option v-for="item in options" :key="item.supplierId" :label="item.supplierName"
                       :value="item.supplierId"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="保险类型:">
          <el-select v-model="queryParams.insuranceType" placeholder="请选择保险类型">
            <el-option v-for="item in dict.type.insurance_type" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="商业险时效:">
          <el-select v-model="queryParams.commercialPolicyValidity" placeholder="请选择商业险时效" style="width: 100%">
            <el-option label="全部" value=""/>
            <el-option v-for="item in dict.type.ageing_type" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="交强险公司:">
          <el-select v-model="queryParams.compulsoryCompany" style="width: 100%" placeholder="请选择交强险公司">
            <el-option v-for="item in options" :key="item.supplierId" :label="item.supplierName"
                       :value="item.supplierId"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="交强险时效:">
          <el-select v-model="queryParams.compulPolicyValidity" placeholder="请选择商业险时效" style="width: 100%">
            <el-option label="全部" value=""/>
            <el-option v-for="item in dict.type.ageing_type" :key="item.value" :label="item.label"
                       :value="item.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="保单号:" prop="insuranceNo">
          <el-input v-model="queryParams.insuranceNo" clearable placeholder="请输入保单号"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="数据维度:">
          <el-select v-model="queryParams.historyFlag" placeholder="请选择数据维度">
            <el-option label="历史记录" value=""/>
            <el-option label="当前保险" :value="0"/>
<!--            <el-option label="过期" :value="2"/>-->
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="交强险付款时间:">
          <el-date-picker v-model="dateJqx" type="daterange" range-separator="-" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd" clearable @change="changeJqxDate" />
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="商业险付款时间:">
          <el-date-picker v-model="dateSyx" type="daterange" range-separator="-" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd" clearable @change="changeSyxDate" />
        </el-form-item>
      </el-col>
    </SearchContent>
    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <el-button size="mini" v-hasPermi="['bxgl:insureManageList:derive']" @click="handleExprot"
                   type="primary">导出
        </el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @setSizeCurrent="getList" border>
        <template slot="tableColAc">
          <el-table-column label="操作" align="center" fixed="left" width="120">
            <template #default="{ row }">
              <el-button size="mini" type="text" @click="viewOpen(row)"
                         v-hasPermi="['bxgl:insureManageList:view']">查看
              </el-button>

              <el-button size="mini" type="text" @click="handleAlter(row)"
                         v-if="Number(row.insuranceStatus) === 4"
                         v-hasPermi="['bxgl:insureManageList:change']">变更
              </el-button>

              <el-button size="mini" type="text" @click="handleDele(row)"
                         v-hasPermi="['bxgl:insureManageList:dele']">删除
              </el-button>
            </template>
          </el-table-column>
        </template>

        <template slot="tableCol">
          <el-table-column align="center" label="保险状态" show-overflow-tooltip min-width="130"
                           prop="insuranceStatus">
            <template slot-scope="{ row }">
              {{
                selectDictLabel(dict.type.insurance_status, row.insuranceStatus)
              }}
            </template>

          </el-table-column>
          <el-table-column align="center" label="保险类型" show-overflow-tooltip min-width="130" prop="insuranceType">

            <template slot-scope="{ row }">
              {{
                selectDictLabel(dict.type.insurance_type, row.insuranceType)
              }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="车牌号" show-overflow-tooltip min-width="100" prop="carNo"/>
          <el-table-column align="center" label="车架号" show-overflow-tooltip min-width="180" prop="vinNo"/>
          <el-table-column align="center" label="品牌/车系/车型" show-overflow-tooltip min-width="320">
            <template #default="{ row }">
              {{ row.brandName }}{{
                row.seriesName ? "/" + row.seriesName : ""
              }}{{ row.modelName ? "/" + row.modelName : "" }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="车辆产权" show-overflow-tooltip min-width="160"
                           prop="propertyRightName"/>
          <el-table-column align="center" label="车辆状态" show-overflow-tooltip min-width="160" prop="businessType">
            <template slot-scope="{ row }">
              {{
                selectDictLabel(dict.type.car_business_type, row.businessType)
              }}
            </template>
          </el-table-column>
          <!--          <el-table-column align="center" label="租车方名称" show-overflow-tooltip   min-width="120" prop="ascription"/>-->
          <el-table-column align="center" label="车船税" show-overflow-tooltip min-width="100" prop="carTax"/>
          <el-table-column align="center" label="总保费" show-overflow-tooltip min-width="100" prop="totalAmount"/>


          <!--交强险-->
          <el-table-column label="交强险" align="center">
            <el-table-column align="center" label="保单号" show-overflow-tooltip min-width="140" prop="jqxPolicyCode"/>
            <el-table-column align="center" label="保险公司" show-overflow-tooltip min-width="140" prop="jqxCompany"/>
            <el-table-column align="center" label="保单状态" show-overflow-tooltip min-width="100" prop="jqxAging"/>
            <el-table-column align="center" label="开始时间" show-overflow-tooltip min-width="120"
                             prop="jqxActualBeginTime"/>
            <el-table-column align="center" label="结束时间" show-overflow-tooltip min-width="120"
                             prop="jqxActualEndTime"/>
            <el-table-column align="center" label="交强险付款时间" show-overflow-tooltip min-width="140"
                             prop="jqxPaymentTime"/>
            <el-table-column align="center" label="费用/元" show-overflow-tooltip min-width="100" prop="jqxAmount"/>
          </el-table-column>

          <!--商业险-->
          <el-table-column label="商业险" align="center">
            <el-table-column align="center" label="保单号" show-overflow-tooltip min-width="140" prop="syxPolicyCode"/>
            <el-table-column align="center" label="保险公司" show-overflow-tooltip min-width="140" prop="syxCompany"/>
            <el-table-column align="center" label="保单状态" show-overflow-tooltip min-width="100" prop="syxAging"/>
            <el-table-column align="center" label="险种" show-overflow-tooltip min-width="120" prop="insuranceProName"/>
            <el-table-column align="center" label="开始时间" show-overflow-tooltip min-width="120"
                             prop="syxActualBeginTime"/>
            <el-table-column align="center" label="结束时间" show-overflow-tooltip min-width="120"
                             prop="syxActualEndTime"/>
            <el-table-column align="center" label="商业险付款时间" show-overflow-tooltip min-width="140"
                             prop="syxPaymentTime"/>
            <el-table-column align="center" label="总费用/元" show-overflow-tooltip min-width="100" prop="syxAmount"/>
          </el-table-column>

          <!--非车险种-->
          <el-table-column label="非车险种" align="center">
            <el-table-column align="center" label="非车险种备注" show-overflow-tooltip min-width="140" prop="otherInsuranceRemark"/>
            <el-table-column align="center" label="非车险险种保费" show-overflow-tooltip min-width="140" prop="otherInsuranceSumAmount"/>
          </el-table-column>

          <!--退保-->
          <el-table-column label="退保" align="center">
            <el-table-column align="center" label="退保保费" show-overflow-tooltip min-width="120"
                             prop="refundInsuranceAmount"/>
            <el-table-column align="center" label="退保生效日期" show-overflow-tooltip min-width="120"
                             prop="refundInsuranceTime"/>
          </el-table-column>

        </template>
      </table-page>
    </div>
  </div>
</template>

<script>
import {getDataList, deleteInsurance} from "@/api/zcwh/insureManageApi.js";

import {getToken} from "@/utils/auth";

import LinkageSelect from "../../components/linkageSelect";
import SearchContent from "@/components/SearchContent/index.vue";
import {listDept} from "@/api/system/dept";
import {getInsuranceSelectList} from "@/api/cwgd/insuranceSelect";
import item from "@/layout/components/Sidebar/Item.vue";

export default {
  name: "insureManageList",

  dicts: [
    "car_business_type",
    "ageing_type",
    "work_order_type",
    "contract_type",
    "car_type",
    "inspection_status",
    "insurance_status",
    "insurance_type",
    "insurance_status"
  ],
  components: {
    LinkageSelect,
    SearchContent,
  },
  computed: {},
  data() {
    return {
      checked: "",
      dateValue: [],
      dateValueNext: [],
      linkageList: [],
      citySelection: [],

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo: "",
        ascriptionId: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        validityStatus: "",
        contractType: "",
        operationsName: "",
        customerName: "",
        nextStartTime: "",
        nextEndTime: "",
        startTime: "",
        endTime: "",
        insuranceType: "",
        insuranceNo: "",
        compulPolicyValidity: "",
        commercialPolicyValidity: '',
        historyFlag:'',
        syxPaymentStartTime:'',
        syxPaymentEndTime:'',
        jqxPaymentStartTime:'',
        jqxPaymentEndTime:''
      },

      total: 0,
      dataList: [],
      createTime: [],
      time: [],
      companyOriginList: [],
      options: [],
      dateJqx:[],
      dateSyx:[]
    };
  },
  created() {
    this.getList();
    this.getCompanyOrigin();
    this.handleList()
  },
  methods: {
    changeJqxDate(val) {
      if (val) {
        this.queryParams.jqxPaymentStartTime = val[0];
        this.queryParams.jqxPaymentEndTime = val[1];
      } else {
        this.queryParams.jqxPaymentStartTime = "";
        this.queryParams.jqxPaymentEndTime = "";
      }
    },

    changeSyxDate(val) {
      if (val) {
        this.queryParams.syxPaymentStartTime = val[0];
        this.queryParams.syxPaymentEndTime = val[1];
      } else {
        this.queryParams.syxPaymentStartTime = "";
        this.queryParams.syxPaymentEndTime = "";
      }
    },

    getCompanyOrigin() {
      listDept({
        deptType: "2", //组织类型（1部门 2公司）
        regionId: null, //所在城市（省市区id）
      }).then((res) => {
        this.companyOriginList = res.data;
        this.belongCompanyList = res.data;
      });
    },
    handleList() {
      getInsuranceSelectList({businessTypeCode: 5}).then(res => {
        if (res.code === 200) {
          this.options = res.data
        }
      })
    },

    isButIdx(idx) {
      this.checked = idx;
      this.dateValue = [];
    },
    dateChangeType(val) {
      if (val) {
        this.queryParams.startTime = val[0];
        this.queryParams.endTime = val[1];
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
    },
    // 时间段选择
    changeDate(val) {
      this.checked = -1;

      if (val) {
        this.queryParams.startTime = val[0];
        this.queryParams.endTime = val[1];
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
      }
    },
    changeDateNext(val) {
      if (val) {
        this.queryParams.nextStartTime = val[0];
        this.queryParams.nextEndTime = val[1];
      } else {
        this.queryParams.nextStartTime = "";
        this.queryParams.nextEndTime = "";
      }
    },
    // 品牌/车系/车型选择
    linkageChange(val) {
      this.linkageList = val;
      if (val) {
        this.queryParams.brandName = val[0] ? val[0].name : "";
        this.queryParams.seriesName = val[1] ? val[1].name : "";
        this.queryParams.modelName = val[2] ? val[2].name : "";
      } else {
        this.queryParams.brandName = "";
        this.queryParams.seriesName = "";
        this.queryParams.modelName = "";
      }
      this.$forceUpdate();
    },

    // 到期日
    timeChange(val) {
    },

    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      getDataList(this.queryParams).then((res) => {
        if (res.code === 200) {
          this.dataList = res.rows;
          this.total = res.total;
          this.loading = false;
        }
      }).catch(() => {
        this.loading = false;
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        carNoVinNo: "",
        ascriptionId: "",
        brandName: "",
        seriesName: "",
        modelName: "",
        validityStatus: "",
        contractType: "",
        operationsName: "",
        customerName: "",
        nextStartTime: "",
        nextEndTime: "",
        startTime: "",
        endTime: "",
        insuranceType: "",
        insuranceNo: "",
        compulPolicyValidity: "",
        commercialPolicyValidity: '',
        historyFlag:0,
        syxPaymentStartTime:'',
        syxPaymentEndTime:'',
        jqxPaymentStartTime:'',
        jqxPaymentEndTime:''
      }
      this.linkageList = []
      this.dateJqx = []
      this.dateSyx = []
      this.getList();
    },

    viewOpen(row, type) {
      this.$router.push({
        name: "InsureManageDetails",
        query: {
          id: row.insuranceCode,
          carId:row.carId,
        },
      });
    },

    handleAlter(row) {
      this.$router.push({
        name: 'InsuranceAlter',
        query: {
          type: 'edit',
          id: row.carId,
        }
      })
    },

    handleDele(row) {
      this.$confirm('此操作将永久删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteInsurance([row.jqxId]).then(res => {
          if (res.code === 200) {

            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },

    handleExprot() {
      this.download(
          "/car/insurance/export",
          {...this.queryParams},
          `保险管理列表_${new Date().getTime()}.xlsx`
      )
    }
  },
};
</script>

<style lang="scss" scoped>
.el-form {
  padding: 18px 20px;
  padding-bottom: 8px;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 16px;

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }

  ::v-deep .el-form-item__label {
    font-size: 14px;
    font-family: 400;
  }
}

::v-deep .el-table {
  &__fixed, &__fixed-right {
    pointer-events: none;

    td {
      pointer-events: auto;
    }
  }
}

</style>
